﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using AmiangDAL;
using AmiangDTO;
using System.IO;

namespace AmiangProject
{
    public partial class ucDM_MatHang : DevExpress.XtraEditors.XtraUserControl
    {
        BanHang2DataContext dbs = new BanHang2DataContext();
        public ucDM_MatHang()
        {
            InitializeComponent();
        }
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
        private void ucDM_MatHang_Load(object sender, EventArgs e)
        {
            var data = from n in dbs.MATHANGs
                       select new
                       {
                           MaMH = n.MaMH,
                           TenMH = n.TenMH,
                           TenNSX = n.NHASANXUAT.TenNSX,
                           TenSIZE = n.SIZE.TenSIZE,
                           DonGiaBanLe = n.DonGiaBanLe,
                           DonGiaBanSi = n.DonGiaBanSi,
                           SoLuongTrongKho = n.SoLuongTrongKho,
                           SoLuongDaBan = n.SoLuongDaBan,
                           TinhTrang = n.TinhTrang,
                           MoTa = n.MoTa,
                           HinhAnh=n.HinhAnh
                       };
            dgvMatHang.DataSource = data;
            dgvMatHang_Click(null, null);

        }

        private void dgvMatHang_Click(object sender, EventArgs e)
        {
            try
            {
                int r = gridView1.FocusedRowHandle;
                
                lblMaMH.Text = gridView1.GetRowCellValue(r, gridColumn1).ToString();
                lblTenMH.Text = gridView1.GetRowCellValue(r, gridColumn2).ToString();
                lblSIZE.Text = gridView1.GetRowCellValue(r, gridColumn4).ToString();
                lblNhaSanXuat.Text = gridView1.GetRowCellValue(r, gridColumn3).ToString();
                lblDonGiaBanSi.Text = gridView1.GetRowCellValue(r, gridColumn5).ToString();
                lblDonGiaBanLe.Text = gridView1.GetRowCellValue(r, gridColumn6).ToString();
                lblTinhTrang.Text = Convert.ToBoolean(gridView1.GetRowCellValue(r, gridColumn11).ToString()) == true ?
                    "Còn kinh doanh" : "Không kinh doanh";
                lblMoTa.Text = gridView1.GetRowCellValue(r, gridView1.Columns["MoTa"]).ToString();
                MATHANG mathang = (from n in dbs.MATHANGs
                                   where n.MaMH == lblMaMH.Text
                                   select n).First();
                byte[] arrImage = mathang.HinhAnh.ToArray();
                picHinh.Image = byteArrayToImage(arrImage);
            }
            catch (NullReferenceException)
            {

            }
            
        }
    }
}
